﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

namespace Client
{
    #region -  COMMAND  -
    public enum Command
    {
        Text = 0,   //message
        Cmd,        //command
        Data,       //File or data byte array
        Name,       //Computer Name
        USBDectect
    };
    #endregion

    #region -  COMMAND TYPE  -
    public enum CommandType
    {
        Standby = 0,
        Shutdown,
        Restart,
        LogOff,
        Hibernate,
        LockComputer,
        ViewClientDesktop,
        GetListProcess,
        DenyProcess,
        UnDenyProcess,
        KillProcess,
        AttendenceCheck,
        StopGetListProcess
    };
    #endregion

    #region -  DATA PACKAGE  -
    public class DataPackage
    {
        private List<byte> listData = null;
        private Command cmd;
        //private string ip;
        private string filename;
        private int filenameLength = 0;
        private int dataLength = 0;

        internal Command Cmd
        {
            get { return cmd; }
            set { cmd = value; }
        }       
        public string Filename
        {
            get { return filename; }
            set { filename = value; }
        }
        public int DataLength
        {
            get { return dataLength; }
            set { dataLength = value; }
        }      

        public DataPackage()
        { }

        public DataPackage(Command cmd, string filename, int dataLength)
        {
            this.Cmd = cmd;
            this.Filename = filename;
            this.DataLength = dataLength;
        }

        public void PackageHeader(byte[] data)
        {
            try
            {
                this.Cmd = (Command)BitConverter.ToInt32(data, 0);
                this.filenameLength = BitConverter.ToInt32(data, 4);
                this.DataLength = BitConverter.ToInt32(data, 8);
                if (filenameLength > 0)
                {
                    this.Filename = Encoding.UTF8.GetString(data, 12, filenameLength);
                }
                else
                {
                    this.Filename = null;
                }
            }
            catch{};
        }

        public byte[] ToByteHeader()
        {
            try
            {
                this.listData = new List<byte>();
                //1  Add command
                this.listData.AddRange(BitConverter.GetBytes((int)this.Cmd));
                //2  Add Length Of Filename
                if (this.Filename != null)
                {
                    this.listData.AddRange(BitConverter.GetBytes(this.Filename.Length));
                }
                else
                {
                    this.listData.AddRange(BitConverter.GetBytes(0));
                }
                //3  Add Data Length
                this.listData.AddRange(BitConverter.GetBytes(this.DataLength));
                //4  Add Filename
                if (this.Filename != null)
                {
                    this.listData.AddRange(Encoding.UTF8.GetBytes(this.Filename));
                }

                return this.listData.ToArray();
            }
            catch
            {
                return null;
            }
        }
    }
    #endregion
}
